package model

//RawSpindle 结构体
type Raw_spindle_status struct {
	Tid        int
	Machine_ip string
	Offset     uint16
	Count      uint16
	Raw_data   []byte
	Dtime      string
}

// 解析 Raw_machine_conf 原始数据
// 将 原始纱锭状态数据 过滤出来， 保存到 cache 中
func (r *Raw_spindle_status) Interpret(machine *Base_machine) {

	length := uint16(len(r.Raw_data))
	var tail uint16 = 1
	if (length % 2) == 0 {
		tail = 0
	}

	var loop_times uint16 = uint16(length/2) + tail
	if r.Count < loop_times {
		loop_times = r.Count
	}

	var position = int(r.Offset) * 4
	var time uint16 = 0
	for ; time < loop_times; time++ {
		virtual_index0 := machine.Convert_2_virtual_spin_no(uint(position + 1))
		virtual_index1 := machine.Convert_2_virtual_spin_no(uint(position + 2))
		virtual_index2 := machine.Convert_2_virtual_spin_no(uint(position + 3))
		virtual_index3 := machine.Convert_2_virtual_spin_no(uint(position + 4))

		if (time*2+1) < length && (r.Raw_data[time*2+1]&0x0f) > 0 {
			machine.store_spindle_status(virtual_index0, uint(r.Raw_data[time*2+1]&0x0f))
		}
		if (time*2+1) < length && ((r.Raw_data[time*2+1]&0xf0)>>4) > 0 {
			machine.store_spindle_status(virtual_index1, uint((r.Raw_data[time*2+1]&0xf0)>>4))
		}
		if (time*2) < length && (r.Raw_data[time*2]&0x0f) > 0 {
			machine.store_spindle_status(virtual_index2, uint(r.Raw_data[time*2]&0x0f))
		}
		if (time*2) < length && ((r.Raw_data[time*2]&0xf0)>>4) > 0 {
			machine.store_spindle_status(virtual_index3, uint((r.Raw_data[time*2]&0xf0)>>4))
		}

		position += 4
	}
}
